##
#   Project: esimplyscript  
#   Author: Mario Guerriero <mefrio.g@gmail.com>
#   Copyright: 2011 Mario Guerriero
#   License: GPL-3+
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 3 of the License, or (at your option)
#  any later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
#
# On Debian GNU/Linux systems, the full text of the GNU General Public License
# can be found in the file /usr/share/common-licenses/GPL-3.
##

import pygtk, gtk, gtk.glade 	#gtk modules

import os	#others modules
import shutil
import funcmain
import handlepaths

class eSimplyScript():
	def __init__(self):
		#load glade file
		self.MainGlade = gtk.glade.XML("../ui/esimplyscript.glade")
		self.wndMain = self.MainGlade.get_widget("wndMain")
		self.wndWait = self.MainGlade.get_widget("wndWait")
		self.ckSwitchboard = self.MainGlade.get_widget("ckSwitchboard")
		self.ckColors = self.MainGlade.get_widget("ckColors")
		self.ckPlank = self.MainGlade.get_widget("ckPlank")
		self.ckWingpanel = self.MainGlade.get_widget("ckWingpanel")
		self.ckSlingshot = self.MainGlade.get_widget("ckSlingshot")
		self.ckMaya = self.MainGlade.get_widget("ckMaya")
		
		#signals
		signals = {
			"on_wndMain_destroy":self.on_wndMain_destroy,
			"on_btnInfo_clicked":self.on_btnInfo_clicked,
			"on_btnInfo_clicked":self.on_btnInfo_clicked,
			"on_btnInfo_clicked":self.on_btnInfo_clicked,
			"on_btnInfo_clicked":self.on_btnInfo_clicked,
			"on_btnDep_clicked":self.on_btnDep_clicked,
			"on_btnDo_clicked":self.on_btnDo_clicked
		}
		
		self.MainGlade.signal_autoconnect(signals)		
		
		#show the window
		if self.wndMain:
			self.wndMain.show()
			gtk.main()
		else:
			print "Error"
			
	#functions
	def on_wndMain_destroy(self, data=None):
		gtk.main_quit()
	
	#buttons
	def on_btnInfo_clicked(self, data=None):
		Info()
	
	def on_btnDep_clicked(self, data=None):
		#self.wndWait.show()
		funcmain.dependence()
		#self.wndWait.hide()
	
	def on_btnDo_clicked(self, data=None):
		self.ckActiveList = []
		
		if self.ckSwitchboard.get_active():
			self.ckActiveList.append(1)
		
		if self.ckColors.get_active():
			self.ckActiveList.append(1)	
		
		if self.ckPlank.get_active():
			self.ckActiveList.append(1)		
		
		if self.ckWingpanel.get_active():
			self.ckActiveList.append(1)	
		
		if self.ckSlingshot.get_active():
			self.ckActiveList.append(1)	
		
		if self.ckMaya.get_active():
			self.ckActiveList.append(1)

		if len(self.ckActiveList) > 1 or len(self.ckActiveList) == 0:
			print "Selct one item"	
		else:
			self.ControllFolders()
			
	#
	def ControllFolders(self):
		if os.path.exists("/home/"+os.getlogin()+"/switchboard"):
			shutil.rmtree("/home/"+os.getlogin()+"/switchboard")
		elif os.path.exists("/home/"+os.getlogin()+"/colors"):
			shutil.rmtree("/home/"+os.getlogin()+"/colors")
		elif os.path.exists("/home/"+os.getlogin()+"/plank"):
			shutil.rmtree("/home/"+os.getlogin()+"/plank")
		elif os.path.exists("/home/"+os.getlogin()+"/wingpanel"):
			shutil.rmtree("/home/"+os.getlogin()+"/wingpanel")		
		elif os.path.exists("/home/"+os.getlogin()+"/slingshot"):	
			shutil.rmtree("/home/"+os.getlogin()+"/slingshot")	
		elif os.path.exists("/home/"+os.getlogin()+"/maya"):
			shutil.rmtree("/home/"+os.getlogin()+"/maya")
			
		self.DownloadUpdate(self.ControllSW())
	
	#
	def ControllSW(self):
		if self.ckSwitchboard.get_active():
			return "switchboard"
		if self.ckColors.get_active():
			return "colors"
		if self.ckPlank.get_active():
			return "plank"		
		if self.ckWingpanel.get_active():
			return "wingpanel"
		if self.ckSlingshot.get_active():
			return "slingshot"	
		if self.ckMaya.get_active():
			return "maya"
	#		
	def DownloadUpdate(self, sw):
		if sw == "switchboard":
			funcmain.switchboard()
			
		elif sw == "colors":
			funcmain.colors()	
			
		elif sw == "plank":
			funcmain.plank()	
			
		elif sw == "wingpanel":
			funcmain.wingpanel()	
			
		elif sw == "slingshot":
			funcmain.slingshot()	
			
		elif sw == "maya":
			funcmain.maya()
		
		
		
class Info(gtk.AboutDialog):
	def __init__(self):
		gtk.AboutDialog.__init__(self)
		self.set_name(handlepaths.APP_TITLE)
		self.set_title(handlepaths.APP_TITLE)
		self.set_program_name(handlepaths.APP_TITLE)
		self.set_version(handlepaths.VERSION)
		icon=gtk.gdk.pixbuf_new_from_file("../icons/esimplyscript.png")
		self.set_icon(icon)
		self.set_logo(icon)
		self.set_copyright('(c) 2011')
		self.set_website_label('Official website')
		self.set_website(handlepaths.APP_WEBSITE)
		self.set_comments(handlepaths.APP_DESCRIPTION)
		self.set_authors(['Mario Guerrierio <mefrio.g@gmail.com>\nGusions <gusions@gmail.com>'])
		self.run()
		self.destroy() 
				
	
			
eSimplyScript()
